package service

import (
	"context"
	"encoding/json"
	"time"
	"zk3/petpal-service/handler/model"
	intis "zk3/petpal-service/inits"
	__ "zk3/petpal-service/proto"
)

type Service struct {
	__.UnimplementedBeautyServer
}

func (s Service) CreateOrder(_ context.Context, in *__.CreateOrderReq) (*__.CreateOrderResp, error) {

	var order model.Order
	model.FindOrder(&order, in.OrderSn)
	if order.ID != 0 {
		return &__.CreateOrderResp{
			Code: 0.,
			Msg:  "该订单已提交请勿重复提交",
		}, nil
	}
	if in.Query <= 0 {
		return &__.CreateOrderResp{
			Code: 0.,
			Msg:  "购买数量不能小于0",
		}, nil
	}
	if in.Query > 5 {
		return &__.CreateOrderResp{
			Code: 0.,
			Msg:  "购买数量不能大于5",
		}, nil
	}
	order = model.Order{
		UserID:    int(in.UserID),
		PackageId: int(in.PackageId),
		OrderSn:   in.OrderSn,
		Query:     int(in.Query),
	}
	err := model.CreateOrder(&order)
	if err != nil {
		return &__.CreateOrderResp{
			Code: 0,
			Msg:  "订单创建失败",
		}, nil
	}
	return &__.CreateOrderResp{
		Code: 200,
		Msg:  "订单创建成功",
	}, nil
}

func (s Service) ListAddress(_ context.Context, in *__.ListAddressReq) (*__.ListAddressResp, error) {
	var Address []model.Address

	err := model.FindAddress(&Address)
	if err != nil {
		return nil, err
	}
	var AddressList []*__.ListAddress
	for _, address := range Address {

		list := &__.ListAddress{
			Name:     address.Name,
			Tel:      address.Tel,
			AddressX: address.AddressX,
		}
		AddressList = append(AddressList, list)
	}
	return &__.ListAddressResp{
		Code: 200,
		Msg:  "查询成功",
		List: AddressList,
	}, nil
}

func (s Service) CreatePackage(_ context.Context, in *__.CreatePackageReq) (*__.CreatePackageResp, error) {

	var PetPal model.PetPal
	model.FindPetPal(&PetPal, int(PetPal.ID))
	if PetPal.ID != 0 {

		Package := model.Package{
			Name:     in.Name,
			PetPalID: int(in.PetPalID),
			Address:  in.Address,
			Store:    in.Store,
			Price:    float64(in.Price),
		}
		err := model.Create(&Package)
		if err != nil {
			return &__.CreatePackageResp{
				Code: 0,
				Msg:  "添加失败",
			}, nil
		}
	} else {
		PetPal = model.PetPal{
			Image:  in.Image,
			Name:   in.Name,
			Age:    int(in.Age),
			Sex:    in.Sex,
			Status: in.Status,
			Jy:     in.Jy,
			Ym:     in.Ym,
			Qc:     in.Qc,
		}
		err := model.CreatePetPal(&PetPal)
		if err != nil {
			return &__.CreatePackageResp{
				Code: 0,
				Msg:  "添加失败",
			}, nil
		}
	}
	return &__.CreatePackageResp{
		Code: 200,
		Msg:  "添加成功",
	}, nil
}

func (s Service) ShowBeauty(_ context.Context, in *__.ShowBeautyReq) (*__.ShowBeautyResp, error) {

	var Beauty model.Beauty
	err := model.Show(&Beauty, int(in.ID))
	if err != nil {
		return nil, err
	}
	marshal, err := json.Marshal(&Beauty)
	if err != nil {
		return nil, err
	}
	intis.Rdb.Set(intis.Ctx, "BeautyID", marshal, time.Hour*3)
	return &__.ShowBeautyResp{
		Name:       Beauty.Name,
		Image:      Beauty.Image,
		BeautyName: Beauty.BeautyName,
		Content:    Beauty.Content,
		Price:      float32(Beauty.Price),
	}, nil
}

func (s Service) Beauty(_ context.Context, in *__.ListBeautyReq) (*__.ListBeautyResp, error) {

	var Beauty []model.Beauty
	err := model.Find(&Beauty)
	if err != nil {
		return &__.ListBeautyResp{
			Code: 0,
			Msg:  "查询失败",
			List: nil,
		}, nil
	}
	var BeautyList []*__.ListBeauty
	for _, b := range Beauty {

		list := &__.ListBeauty{
			Name:       b.Name,
			Image:      b.Image,
			BeautyName: b.BeautyName,
			Content:    b.Content,
			Price:      float32(b.Price),
		}
		BeautyList = append(BeautyList, list)
	}
	marshal, err := json.Marshal(&Beauty)
	if err != nil {
		return nil, err
	}
	intis.Rdb.Set(intis.Ctx, "beauty", marshal, time.Hour*3)
	return &__.ListBeautyResp{
		Code: 200,
		Msg:  "查询成功",
		List: BeautyList,
	}, nil

}
